using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using Barrage.Global;
using Barrage.Scenes;
using Spacedash.Game.Nodes;
using Barrage.Elements;
using Barrage.Elements.Cameras;
using Barrage.Audios;

namespace Spacedash.Game.Cameras
{
    /// <summary>
    /// This camera is really a list of cameras, activating only one of them at a time (the user chooses which camera to use)
    /// </summary>
    public class RaceCamera : Camera
    {
        List<Camera> cameras = new List<Camera>();
        int currentCamera = 0;

        ShipNode target;

        /// <summary>
        /// Returns the currently active camera
        /// </summary>
        public Camera CurrentCamera { get { return cameras[currentCamera]; } }

        /// <summary>
        /// Selects the next camera in the list
        /// </summary>
        public void Next()
        {
            currentCamera = (currentCamera + 1) % cameras.Count;
        }

        /// <summary>
        /// \internal Returns the currently selected camera's position
        /// </summary>
        public override Vector3 Position
        {
            get { return CurrentCamera.Position; }
        }

        void FillCameras(ShipNode target,Vector2 viewportSize)
        {
            this.target = target;
            cameras.Add(new PursuitCamera(target, viewportSize));
            BasicFirstPersonCamera camFPS = new BasicFirstPersonCamera(target.Ship.PlayerIndex);
            camFPS.SetViewportSize(viewportSize);
            cameras.Add(camFPS);
            BasicOrbitCamera camOrbit = new BasicOrbitCamera(target.Motion, target.Ship.PlayerIndex);
            camOrbit.SetViewportSize(viewportSize);
            cameras.Add(camOrbit);
        }

        /// <summary>
        /// Constructor. Inits each camera in the list and selects the first one.
        /// </summary>
        /// <param name="target">Ship that will be the target of the cameras</param>
        /// <param name="viewportSize">Viewport size in pixels</param>
        public RaceCamera(ShipNode target,Vector2 viewportSize)
        {
            FillCameras(target,viewportSize);
        }

        /// <summary>
        /// \internal Updates matrices based on selected camera
        /// </summary>
        protected override void DoUpdate()
        {
            Camera camera = CurrentCamera;
            ((SpatializedAudio)target.ShipItem.Audio).Camera = camera;
            camera.Update();
            projection = camera.Projection;
            motion = camera.Motion;
            base.DoUpdate();
        }
    }
}